package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.entity.day04.Kc;
import com.jiyun.entity.day04.KcDTO1;
import com.jiyun.feign.BqFeign;
import com.jiyun.feign.KcFeign;
import com.jiyun.service.KcService;
import com.jiyun.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Title: KcController
 * @Author 杜瑞
 * @Date 2023/8/25 16:19
 */
@RestController
@RequestMapping("/kc")
public class KcController {
    @Autowired
    private BqFeign bqFeign;
    @Autowired
    private KcFeign kcFeign;
    @Autowired
    private KcService kcService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("findBq")
    public ResultResponse findBq(){
        try {
            ResultResponse bq = bqFeign.find();
            return ResultResponse.success(bq);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
     }

     @RequestMapping("findAll")
    public ResultResponse findAll(@RequestBody KcDTO1 kcDTO1) {
        try {
            ResultResponse list = kcFeign.findAll1(kcDTO1);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
     }

    @RequestMapping("findAllDate")
    public ResultResponse findAllDate(@RequestBody KcDTO1 kcDTO1) {
        try {
            ResultResponse list = kcFeign.findAllDate(kcDTO1);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findAllMax")
    public ResultResponse findAllMax(@RequestBody KcDTO1 kcDTO1) {
        try {
            ResultResponse list = kcFeign.findAllMax(kcDTO1);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("likePost")
    public ResultResponse likePost(String name) {
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            if (name1==null){
                redisTemplate.opsForValue().set(name,1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum("1");
                kcService.updateById(kc);
            }else {
                Integer sum=(name1 + 1);
                redisTemplate.opsForValue().set(name, name1 + 1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum(String.valueOf(sum));
                kcService.updateById(kc);
            }
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatelikePost")
    public  ResultResponse updatelikePost(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            redisTemplate.opsForValue().set(name, name1 -1);
            QueryWrapper<Kc> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Kc kc = kcService.getOne(wrapper);
            kc.setZNum(String.valueOf(name1 -1));
            kcService.updateById(kc);
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }


    @RequestMapping("findByKc")
    public ResultResponse findByKc(Long id){
        try {
            Kc kc = kcService.getById(id);
            return ResultResponse.success(kc);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }





 }
